package com.ssy.lingxi.contract.esign.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.constant.RequestMethod;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import com.ssy.lingxi.contract.esign.request.OrganizationSealCreateRequest;
import com.ssy.lingxi.contract.esign.request.PersonalSealCreateRequest;
import com.ssy.lingxi.contract.esign.response.OrganizationSealResponse;
import com.ssy.lingxi.contract.esign.response.PersonalSealCreateResponse;
import com.ssy.lingxi.contract.esign.response.SealDetailResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 印章服务API
 * @author yzc
 * @version 2.0.0
 * @date 2020/8/26
 */
public class Seal {

    private final AuthToken authToken;
    private final EsignProperties properties;

    public Seal(AuthToken authToken, EsignProperties properties) {
        this.authToken = authToken;
        this.properties = properties;
    }


    /**
     * 创建个人模板印章
     */
    public PersonalSealCreateResponse personalSealCreate(PersonalSealCreateRequest request) throws EsignException {
        String resourcePath = "/v1/accounts/{accountId}/seals/personaltemplate";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, PersonalSealCreateResponse.class);
    }

    /**
     * 创建机构模板印章
     */
    public OrganizationSealResponse organizationSealTemplateCreate(OrganizationSealCreateRequest request) throws EsignException {
        String resourcePath = "/v1/organizations/{orgId}/seals/officialtemplate";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, OrganizationSealResponse.class);
    }

    /**
     * 查询个人印章
     */
    public SealDetailResponse personalSealDetail(String accountId) throws EsignException {
        String resourcePath = "/v1/accounts/{accountId}/seals";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("accountId", accountId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, SealDetailResponse.class);
    }

    /**
     * 查询机构印章
     */
    public SealDetailResponse organizationSealDetail(String orgId) throws EsignException {
        String resourcePath = "/v1/organizations/{orgId}/seals";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orgId", orgId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, SealDetailResponse.class);
    }

    /**
     * 删除个人印章
     */
    public void personalSealDelete(String accountId, String sealId) throws EsignException {
        String resourcePath = "/v1/accounts/{accountId}/seals/{sealId}";
        RequestMethod method = RequestMethod.DELETE;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("accountId", accountId);
        paramMap.put("sealId", sealId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

    /**
     * 删除机构印章
     */
    public void organizationSealDelete(String orgId, String sealId) throws EsignException {
        String resourcePath = "/v1/organizations/{orgId}/seals/{sealId}";
        RequestMethod method = RequestMethod.DELETE;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orgId", orgId);
        paramMap.put("sealId", sealId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

}
